package com.kui.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.kui.entity.Folder;
import com.kui.util.DBHelp;
import com.kui.util.RowMapper;

public class FolderDao {
	
	DBHelp<Folder> db = new DBHelp<Folder>();
	
	private class FolderRowMapper implements RowMapper<Folder>{

		public Folder mapperRow(ResultSet rs) throws SQLException {
			Folder f = new Folder();
			f.setId(rs.getString("id"));
			f.setName(rs.getString("name"));
			f.setCreatetime(rs.getString("createtime"));
			f.setT_user_id(rs.getString("t_user_id"));
			f.setT_user_name(rs.getString("t_user_name"));
			
			return f;
		}
		
	}

	public List<Folder> findAll() {
		String sql = "SELECT t_folder.id,t_folder.name,createtime,t_user_id,t_user.username AS t_user_name FROM t_folder LEFT JOIN t_user ON t_user.id = t_user_id";
		return db.executeQueryForList(sql, new FolderRowMapper());
	}

	public void addFolder(Folder f) {
		String sql = "INSERT INTO t_folder(id,name,createtime,t_user_id) VALUES(?,?,?,?)";
		db.executeSQL(sql, f.getId(),f.getName(),f.getCreatetime(),f.getT_user_id());
		
	}

	public Folder findById(String folderid) {
		String sql = "SELECT id,name,createtime,t_user_id,name AS t_user_name FROM t_folder WHERE id = ? ";
		return db.executeQueryForObject(sql, new FolderRowMapper(),folderid);
	}

}
